package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.utils.QiniuUtils;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.UUID;

/**
 * 体检套餐
 */
@RestController
@RequestMapping("/setmeal")
public class SetmealContorller {

    @Reference
    SetmealService setmealService;


    @GetMapping("/findPage")
    public Result findPage(QueryPageBean queryPageBean){

        return setmealService.queryPage(queryPageBean);

    }

    @PostMapping("/upload")
    public Result upload(MultipartFile imgFile){
        try {

            //获取文件名称
            String filename = imgFile.getOriginalFilename();
            //获取文件类型
            String extension =filename.substring(filename.lastIndexOf("."));

            //新文件名称
            String newFileName= UUID.randomUUID().toString().replace("-","")+extension;

            QiniuUtils.upload2Qiniu(imgFile.getBytes(),newFileName);

            System.out.println("filename:   "+newFileName);

            return new Result(true, MessageConstant.PIC_UPLOAD_SUCCESS,newFileName);

        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.PIC_UPLOAD_FAIL);
        }
    }

    @PostMapping("/save")
    public Result save(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){
        return setmealService.add(setmeal,checkGroupIds);

    }

    @GetMapping("/queryCheckGroupIds/{setmealId}")
    public Result queryCheckGroupIds(@PathVariable Integer setmealId){

        return  setmealService.queryCheckGroupIds(setmealId);

    }

    @GetMapping("/querySetmeal/{setmealId}")
    public Result querySetmeal(@PathVariable Integer setmealId){

        return setmealService.queryById(setmealId);
    }

    @PostMapping("/update")
    public Result update(@RequestBody Setmeal setmeal,Integer[] checkGroupIds){

        return setmealService.update(setmeal,checkGroupIds);

    }
    @GetMapping("/delete/{setmealId}")
    public Result delete(@PathVariable Integer setmealId){

        return  setmealService.delete(setmealId);

    }






}
